﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Oracle.DataAccess.Client;
using System.Collections.Generic;

namespace DBtuning
{
    public partial class Param_1 : System.Web.UI.Page
    {
        DataTable params1;
        IDictionary<string, string> dictParamNames = Filereader.GetPerformanceParamNames();
        IDictionary<string, string> dictParamThresholds = Filereader.GetPerformanceParamThresholds();
        IDictionary<string, string> dictParamWarnings = Filereader.GetPerformanceParamWarnings();
        IDictionary<string, string> dictParamDescriptions = Filereader.GetPerformanceParamDescriptions();
        IDictionary<string, string> dictParamUnits = Filereader.GetPerformanceParamUnits();
        protected void Page_Load(object sender, EventArgs e)
        {
            FetchData fetch = new FetchData();
            DataTable dt = fetch.GetOneday();

            params1 = new DataTable("ParamValues");
            DataColumn dKey = params1.Columns.Add("ParameterID", typeof(String));
            params1.Columns.Add("ParameterName", typeof(String));
            params1.Columns.Add("ParameterDesc", typeof(String));
            params1.Columns.Add("ParameterUnit", typeof(String));
            params1.Columns.Add("ParameterThresholdValue", typeof(double));
            params1.Columns.Add("ParameterWarningValue", typeof(double));
            params1.Columns.Add("ParameterValue", typeof(string));
            params1.Columns.Add("ParameterStatus", typeof(String));
            params1.Columns.Add("ParameterPollTimeStamp", typeof(DateTime));
            params1.PrimaryKey = new DataColumn[] { dKey };

            double[] avg;
            avg = new double[7];
            for (int i = 0; i < 7; i++)
                avg[i] = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                avg[0] += Convert.ToDouble(dt.Rows[i]["LIBRARY_CACHE_HIT_RATIO"]);
                avg[1] += Convert.ToDouble(dt.Rows[i]["DICTIONARY_CACHE_HIT_RATIO"]);
                avg[2] += Convert.ToDouble(dt.Rows[i]["BUFFER_CACHE_HIT_RATIO"]);
                avg[3] += Convert.ToDouble(dt.Rows[i]["REDO_LOG_BUFFER_RATIO"]);
                avg[4] += Convert.ToDouble(dt.Rows[i]["REDO_LOG_SPACE_WAIT_TIME"]);
                avg[5] += Convert.ToDouble(dt.Rows[i]["SORT_AREA_RATIO"]);
                avg[6] += Convert.ToDouble(dt.Rows[i]["ROLLBACK_SEGMENT_WAIT_RATIO"]);
            
            }

            int index = 0;
            foreach (string key in dictParamNames.Keys)
            {
                double val = avg[index] / dt.Rows.Count;
                double valThres = double.Parse(dictParamThresholds[key]);
                double valWarning = double.Parse(dictParamWarnings[key]);
                DataRow row = params1.NewRow();
                row[0] = key;
                row[1] = dictParamNames[key];
                row[2] = dictParamDescriptions[key];
                row[3] = dictParamUnits[key];
                row[4] = valThres;
                row[5] = valWarning;
                if (row[3].ToString().Equals("%"))
                    val *= 100.0;
                row[6] = val.ToString("0.00");
                if (index < 3)
                {
                    if (val < valThres)
                        row[7] = "Alert";
                    else if (val < valWarning)
                        row[7] = "Unhealthy";
                    else
                        row[7] = "Healthy";
                }
                else
                {
                    if (val > valThres)
                        row[7] = "Alert";
                    else if (val > valWarning)
                        row[7] = "Unhealthy";
                    else
                        row[7] = "Healthy";
                }
                row[8] = DateTime.Now;
                params1.Rows.Add(row);
                index++;
            }
            GridView_Param.DataSource = params1;
            GridView_Param.DataBind();

            if (params1.Rows[0]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the SHARED_POOL_SIZE." + "<p>";
            }
            if (params1.Rows[1]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the SHARED_POOL_SIZE." + "<p>";
            }
            if (params1.Rows[2]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the DB_BLOCK_BUFFERS." + "<p>";
            }
            if (params1.Rows[3]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the LOG_BUFFER_SIZE." + "<p>";
            }
            if (params1.Rows[4]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the LOG_BUFFER_SIZE." + "<p>";
            }
            if (params1.Rows[5]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Enlarge the SORT_AREA_SIZE." + "<p>";
            }
            if (params1.Rows[6]["ParameterStatus"].ToString() == "Alert")
            {
                Label6.Text += "Add more rollback segments." + "<p>";
            }
        }

        protected void GridView_Param_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.Cells[e.Row.Cells.Count - 2].Text)
            {
                case "Alert":
                    e.Row.ForeColor = System.Drawing.Color.Red;
                    break;
                case "Unhealthy":
                    e.Row.ForeColor = System.Drawing.Color.Orange;
                    break;
                case "Healthy":
                    e.Row.ForeColor = System.Drawing.Color.Green;
                    break;
            }
        }
    }
}
